package com.jeremyfeinstein.socket;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jeremyfeinstein.event.AddAudioStreamEvent;
import com.jeremyfeinstein.event.AddVideoStreamEvent;
import com.jeremyfeinstein.event.AllowTalkEvent;
import com.jeremyfeinstein.event.ApplyForSpeakerEvent;
import com.jeremyfeinstein.event.AttacheFileEvent;
import com.jeremyfeinstein.event.ChangeHostAssistListEvent;
import com.jeremyfeinstein.event.ChangeUserRoleEvent;
import com.jeremyfeinstein.event.ChatMessageEvent;
import com.jeremyfeinstein.event.CloseAudioStreamEvent;
import com.jeremyfeinstein.event.CloseVideoStreamEvent;
import com.jeremyfeinstein.event.DisableAudioEvent;
import com.jeremyfeinstein.event.EnableAudioEvent;
import com.jeremyfeinstein.event.EndMeetingEvent;
import com.jeremyfeinstein.event.ExitMeetingEvent;
import com.jeremyfeinstein.event.ForbidTalkEvent;
import com.jeremyfeinstein.event.GetMeetingUserListEvent;
import com.jeremyfeinstein.event.JoinEvent;
import com.jeremyfeinstein.event.ShareVideoEvent;
import com.jeremyfeinstein.event.StartRecordEvent;
import com.jeremyfeinstein.event.StopShareEvent;
import com.jeremyfeinstein.event.TempRoomEvent;
import com.jeremyfeinstein.exception.YchsException;
import com.jeremyfeinstein.gson.ChatMessage;
import com.jeremyfeinstein.gson.JoinResponse;
import com.jeremyfeinstein.gson.MeetingUser;
import com.jeremyfeinstein.gson.OnResponse;
import com.jeremyfeinstein.gson.RecordFile;
import com.jeremyfeinstein.gson.ShareVideo;
import com.jeremyfeinstein.listener.OnConferenceSocketListener;
import com.jeremyfeinstein.listener.OnSocketListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;



/**
 * Created by sunxunchao on 2019/1/14.
 */
public class YCHSSocketClient extends SimpleSocketClient {
    private static final String TAG = YCHSSocketClient.class.getSimpleName();
    private Gson gson;

    public YCHSSocketClient(String url) {
        super(url);
        gson = new Gson();
    }

    @Override
    public void socketResponse(OnSocketListener onSocketListener, String type, JSONObject json) {
        Log.d(TAG, "socketResponse: type ===>" + type + "\n msg===>" + json.toString());
        try {

            if (SocketType.TYPE_GetMeetingUserList.equals(type) || SocketType.TYPE_GetMeetingUserListAlone.equals(type)) {
                GetMeetingUserListEvent event = new GetMeetingUserListEvent();
                JSONArray userList = json.getJSONArray("userList");
                List<MeetingUser> meetingUsers = new ArrayList<MeetingUser>();
                MeetingUser meetingUser;
                Gson gson = new GsonBuilder().create();
                for (int i = 0; i < userList.length(); i++) {
                    meetingUser = gson.fromJson(userList.get(i).toString(), MeetingUser.class);
                    meetingUsers.add(meetingUser);
                }
                event.setUserList(meetingUsers);
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_ExitMeeting.equals(type)) {
                ExitMeetingEvent event = new ExitMeetingEvent();
                JSONArray userList = json.getJSONArray("userList");
                String userCode = json.getString("userCode");
                String message = json.getString("message");
                List<MeetingUser> meetingUsers = new ArrayList<MeetingUser>();
                MeetingUser meetingUser;
                Gson gson = new GsonBuilder().create();
                for (int i = 0; i < userList.length(); i++) {
                    meetingUser = gson.fromJson(userList.get(i).toString(),
                            MeetingUser.class);
                    meetingUsers.add(meetingUser);
                }
                event.setUserCode(userCode);
                event.setMessage(message);
                event.setUserList(meetingUsers);
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_MeetingModeSetting.equals(type)) {
                TempRoomEvent event = gson.fromJson(json.toString(),
                        TempRoomEvent.class);
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_forbidTalkStream.equals(type)) {
                ForbidTalkEvent event = gson.fromJson(json.toString(),
                        ForbidTalkEvent.class);
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_allowTalkStream.equals(type)) {
                AllowTalkEvent event = gson.fromJson(json.toString(), AllowTalkEvent.class);
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_ChatMessage.equals(type)) {
                ChatMessageEvent event = new ChatMessageEvent();
                ChatMessage chatMessage;
                Gson gson = new GsonBuilder().create();
                Log.e("chatmessage", json.toString());
                chatMessage = gson.fromJson(json.toString(), ChatMessage.class);
                event.setChatMessage(chatMessage);
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_Join.equals(type)) {
                JoinEvent event = new JoinEvent();
                JoinResponse joinResponse = gson.fromJson(json.toString(),
                        JoinResponse.class);
                event.setJoinResponse(joinResponse);
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_AttacheFile.equals(type)) {
                AttacheFileEvent event = new AttacheFileEvent();
                OnResponse onResponse = gson.fromJson(json.toString(),
                        OnResponse.class);
                event.setOnResponse(onResponse);
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_DisableAudio.equals(type)) {
                DisableAudioEvent event = gson.fromJson(json.toString(),
                        DisableAudioEvent.class);
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_EnableAudio.equals(type)) {
                EnableAudioEvent event = gson.fromJson(json.toString(),
                        EnableAudioEvent.class);
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_ShareVideo.equals(type)) {
                ShareVideoEvent event = new ShareVideoEvent();
                ShareVideo shareVideo = gson.fromJson(json.toString(),
                        ShareVideo.class);
                event.setShareVideo(shareVideo);
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_StopShare.equals(type)
                    || SocketType.TYPE_CloseShare.equals(type)) {
                StopShareEvent event = new StopShareEvent();
                if (json.has("shareWhiteboard")) {
                    event.setShareWhiteboard(true);
                } else {
                    event.setShareWhiteboard(false);
                }
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_changeHostAssistList.equals(type)) {
                ChangeHostAssistListEvent event = new ChangeHostAssistListEvent();
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_ChangeUserRole.equals(type)) {
                ChangeUserRoleEvent event = new ChangeUserRoleEvent();
                event.setMeetingId(json.getString("meetingId"));
                event.setNoticeType(json.getString("noticeType"));
                event.setStatus(json.getString("status"));
                event.setNewRole(json.getString("newRole"));
                event.setUserCode(json.getString("USER_CODE"));
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_ApplyForSpeaker.equals(type)) {
                ApplyForSpeakerEvent event = new ApplyForSpeakerEvent();
                event.setMeetingId(json.getString("meetingId"));
                event.setUserCode(json.getString("userCode"));
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_addVideoStream.equals(type)) {
                AddVideoStreamEvent event = new AddVideoStreamEvent();
                event.setUserCode(json.getString("userCode"));
                Log.d("Video", "addAudio");
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_deleteVideoStream.equals(type)) {
                CloseVideoStreamEvent event = new CloseVideoStreamEvent();
                event.setUserCode(json.getString("userCode"));
                // event.setStreamId(json.getString("streamId"));
                Log.d("Video", "DeleteVideo");
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_deleteAudioStream.equals(type)) {
                CloseAudioStreamEvent event = new CloseAudioStreamEvent();
                event.setUserCode(json.getString("userCode"));
                Log.d("Audio", "DeleteAudio");
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_addAudioStream.equals(type)) {
                AddAudioStreamEvent event = new AddAudioStreamEvent();
                event.setUserCode(json.getString("userCode"));
                Log.d("Audio", "addAudio");
                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_EndMeetingRoom.equals(type)) {
                EndMeetingEvent event = new EndMeetingEvent();
                if (json.has("resultCode")) {
                    event.setResultCode(json.getInt("resultCode"));
                }
                if (json.has("errorMessage")) {
                    event.setErrorMessage(json.getString("errorMessage"));
                }
                if (json.has("message")) {
                    event.setMessage(json.getString("message"));
                }
                if (json.has("err")) {
                    event.setErr(json.getString("err"));
                }

                EventBus.getDefault().post(event);
            } else if (SocketType.TYPE_StartRecord.equals(type)) {
                RecordFile file = gson.fromJson(json.getString("file"),
                        RecordFile.class);
                StartRecordEvent event = new StartRecordEvent();
                event.setFile(file);
                EventBus.getDefault().post(event);
            }
        }catch (Exception e){
            Log.e(TAG, "socketResponse: ", e);
        }
    }
}
